<?php 
session_start();

include_once("priedai/conf.php");
ob_start ();


##################### Auto Atjungimas nuo sistemos ########################

##################### Jei svetaine uzdaryta remontui ir jei kreipiasi ne administratorius ###
if ($conf['Palaikymas'] == 1 && (!defined("LEVEL") || LEVEL < 30)) { redirect("remontas.php"); exit; }

if (!isset($_SESSION['username']) || empty($_SESSION['username'])) {
	header("Content-type: text/html; charset=utf-8");
	die(klaida("Atsiprašome","<h4>Tik registruotiems naraims</h4>Prašome užsiregistruokite mūsų svetainėje. Tik tokiu atveju galėsite siųstis failus iš mūsų serverio"));
}

//Nustatome atsisiuntimo ID
if (isset($url['d']) && isnum($url['d']) && $url['d'] > 0) { $did = (int)$url['d']; } else { $did = 0; }

# FUNKCIJOS
if (isset($did) && $did >0) {
	$sql = mysql_query1("SELECT `file` FROM `".LENTELES_PRIESAGA."siuntiniai` WHERE `ID` = $did LIMIT 0, 1 ");
	if (mysql_affected_rows() > 0) {
		$sql = mysql_fetch_assoc($sql);
		download("downloads/".$sql['file']."",".htaccess|.|..|remontas.php|index.php|config.php|conf.php");
	}
	else { 
		header("Content-type: text/html; charset=utf-8");
		header("HTTP/1.0 404 Not Found");
		die(klaida("Atsiprašome","<h4>Failas neegzistuoja</h4>Prašome apie blogą siuntinį <a href='report.php?type=download&id=".$did."'>pranešti</a> administracijai"));
	}
}

//Siunciam nurodyta faila i narsykle. Pratestavau ant visu operaciniu ir narsykliu.
function download($file, $filter) {
    $filter = explode("|", $filter);
    if (!in_array($file, $filter) && is_file($file)) {
		if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) { $file = preg_replace('/\./', '%2e', $file,substr_count($file, '.') - 1);	}
		if (is_file($file)) {
			if(connection_status()==0){
				  if (get_user_os() == "MAC") {
					header("Content-Type: application/x-unknown\n");
					header("Content-Disposition: attachment; filename=\"".basename($file)."\"\n");
				  }
				  elseif (get_browser_info() == "MSIE") {
					$disposition = (!eregi("\.zip$", basename($file))) ? 'attachment' : 'inline';
					header('Content-Description: File Transfer'); 
					header('Content-Type: application/force-download'); 
					header('Content-Length: ' . (string)(filesize($file))); 				
					header("Content-Disposition: $disposition; filename=\"".basename($file)."\"\n");
					header("Cache-Control: cache, must-revalidate");
					header('Pragma: public');
				  }
				  elseif (get_browser_info() == "OPERA") {
					header("Content-Disposition: attachment; filename=\"".basename($file)."\"\n");
					header("Content-Type: application/octetstream\n");
				  }
				  else {
					header("Content-Disposition: attachment; filename=\"".basename($file)."\"\n");
					header("Content-Type: application/octet-stream\n");
				  }
				  header("Content-Length: ".(string)(filesize($file))."\n\n");
				readfile(''.$file.'');
				exit;
			}else{
				header("location: $_SERVER[PHP_SELF]");
				exit;
			}
		}
		else {
			header("Content-type: text/html; charset=utf-8");
			klaida("Failas nerastas","<h4>Atsiprašome</h4>Tačiau pageidaujamas failas nerastas");
			header("HTTP/1.0 404 Not Found");
		}
    }
    else { header("location: ?id,6"); exit; }
}
?>